Skip to content

feat: Add compare_directories tool for directory comparison#3890

Open
nagual2 wants to merge 11 commits intomodelcontextprotocol:mainfrom
nagual2:feature/compare-directories
Open

feat: Add compare_directories tool for directory comparison#3890
nagual2 wants to merge 11 commits intomodelcontextprotocol:mainfrom
nagual2:feature/compare-directories

Conversation

@nagual2
Copy link
Copy Markdown

@nagual2 nagual2 commented Apr 10, 2026

Summary

Adds compare_directories tool to filesystem MCP server for comparing two directory structures.

New Tool: compare_directories

Compares two directories and returns:

  • onlyInDir1: Files only in first directory
  • onlyInDir2: Files only in second directory
  • differentContent: Files with different size/mtime
  • identical: Identical files

Changes

  • src/filesystem/lib.ts - Added compareDirectories() function
  • src/filesystem/index.ts - Registered tool
  • src/filesystem/__tests__/lib.test.ts - Added 6 tests

Tests

✅ Files unique to each directory
✅ Content difference detection
✅ Identical file identification
✅ Empty directory handling
✅ Nested structures

All tests pass with npm test.

Copy link
Copy Markdown
Member

@olaservo olaservo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution. A couple of implementation issues and a scope question:

Implementation issues:

  1. The compareContent parameter is accepted in the schema and function signature but never used — comparison always uses size/mtime regardless of the flag. Either implement content comparison or remove the parameter.
  2. Files with identical content but different timestamps will be reported as "different," and files with different content but the same size/mtime will be reported as "identical." This should at least be documented clearly, or addressed with actual content hashing when compareContent is true.

Scope question:
Per our CONTRIBUTING.md, we're selective about new features that aren't core to a server's purpose. Directory comparison feels like it could be composed by an LLM using existing tools (directory_tree, read_file), or handled by a dedicated server. Would you consider publishing this as a standalone MCP server on the Registry instead?


This review was assisted by Claude Code.

nagual2 added 2 commits April 13, 2026 19:01
- Add compareFileContents helper for byte-by-byte comparison
- compareContent=true now reads and compares actual file contents
- Files with same content (but different mtime) go to identical
- Fix: compareContent parameter is now actually used
- When sizes differ, skip content comparison for efficiency
- Test same-size different-content detection with compareContent=true
- Test same-content different-mtime detection (should be identical)
- Test binary file content comparison
- Test compareContent=false behavior (mtime-based)
@nagual2
Copy link
Copy Markdown
Author

nagual2 commented Apr 13, 2026

@olaservo Thanks for the detailed review! I've addressed the implementation issues:

Fixed: compareContent parameter now works

Before: The parameter was accepted but ignored — comparison always used size/mtime.

After:

  • compareContent=false (default): Uses size/mtime for quick comparison
  • compareContent=true: Reads and compares actual file contents byte-by-byte

When compareContent=true:

  • Files with identical content but different timestamps are correctly reported as identical (content is what matters)
  • Files with different content but same size are correctly detected as different (no false positives)

Implementation details

Added compareFileContents() helper that:

  1. Reads both files as buffers (parallel via Promise.all)
  2. Early exit if sizes differ
  3. Uses Buffer.equals() for efficient byte-by-byte comparison

Why this belongs in core filesystem server

While directory comparison could theoretically be composed by an LLM using existing tools, this argument applies to many other tools already in the server (e.g., search_files, get_file_info). The practical reality:

  1. Performance: Comparing two directories with 1000 files each would require:

    • directory_tree calls
    • Potentially hundreds of read_file calls for content comparison
    • Thousands of tokens and significant time
  2. Atomicity: The comparison needs to be consistent (directory state shouldn't change mid-comparison)

  3. Real-world usage: Directory comparison is a fundamental filesystem operation — essential for backups, deployments, sync operations, and integrity verification

  4. Ecosystem gap: FAR is Windows-only and not scriptable. Most developers lack a cross-platform directory comparison tool that integrates with their workflow

This follows the pattern of other "composed" tools already in the server — providing efficient, atomic operations that would be impractical to build from lower-level primitives.


Commits:

  • 5ea9b667 — Implement content comparison in compareDirectories
  • 96172ba9 — Add tests for compareContent parameter

All tests pass with npm test. The changes are minimal and focused on fixing the unused parameter issue while maintaining backward compatibility.

nagual2 added 2 commits April 13, 2026 19:45
- save and restore global allowedDirectories state
- fixes tests that expect validatePath to work with test directories
The test was writing to marker.txt expecting diff-mtime.txt mtime to change.
Now it directly re-writes diff-mtime.txt to update its mtime.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants